<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ include file="/WEB-INF/jsp/common/header.jsp" %>

<div class="row">
    <div class="col-12">
        <div class="card shadow mb-4">
            <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
                <h5 class="mb-0">我的博客</h5>
                <a href="${pageContext.request.contextPath}/blog/new" class="btn btn-light btn-sm">
                    <i class="fas fa-plus me-1"></i>写新博客
                </a>
            </div>
            <div class="card-body">
                <c:choose>
                    <c:when test="${not empty blogList}">
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>标题</th>
                                        <th>分类</th>
                                        <th>发布时间</th>
                                        <th>更新时间</th>
                                        <th>阅读量</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <c:forEach var="blog" items="${blogList}">
                                        <tr>
                                            <td>
                                                <a href="${pageContext.request.contextPath}/blog/view?id=${blog.id}" class="text-decoration-none">
                                                    ${blog.title}
                                                </a>
                                            </td>
                                            <td>
                                                <span class="badge bg-${blog.category == '技术' ? 'primary' : 
                                                                blog.category == '生活' ? 'success' : 
                                                                blog.category == '旅游' ? 'info' : 
                                                                blog.category == '艺术' ? 'warning' : 'secondary'}">
                                                    ${blog.category}
                                                </span>
                                            </td>
                                            <td><fmt:formatDate value="${blog.createTime}" pattern="yyyy-MM-dd HH:mm" /></td>
                                            <td><fmt:formatDate value="${blog.updateTime}" pattern="yyyy-MM-dd HH:mm" /></td>
                                            <td>${blog.viewCount}</td>
                                            <td>
                                                <div class="btn-group btn-group-sm" role="group">
                                                    <a href="${pageContext.request.contextPath}/blog/view?id=${blog.id}" class="btn btn-info" title="查看">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                    <a href="${pageContext.request.contextPath}/blog/edit?id=${blog.id}" class="btn btn-warning" title="编辑">
                                                        <i class="fas fa-edit"></i>
                                                    </a>
                                                    <button type="button" class="btn btn-danger" onclick="confirmDelete(${blog.id})" title="删除">
                                                        <i class="fas fa-trash"></i>
                                                    </button>
                                                </div>
                                            </td>
                                        </tr>
                                    </c:forEach>
                                </tbody>
                            </table>
                        </div>
                    </c:when>
                    <c:otherwise>
                        <div class="alert alert-info text-center">
                            <p class="mb-3">您还没有发布过博客</p>
                            <a href="${pageContext.request.contextPath}/blog/new" class="btn btn-primary">
                                <i class="fas fa-plus me-1"></i>发布第一篇博客
                            </a>
                        </div>
                    </c:otherwise>
                </c:choose>
            </div>
        </div>
    </div>
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteModalLabel">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                确定要删除这篇博客吗？此操作不可恢复。
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteForm" action="${pageContext.request.contextPath}/blog/delete" method="post">
                    <input type="hidden" id="deleteId" name="id">
                    <button type="submit" class="btn btn-danger">确认删除</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
// 删除确认
function confirmDelete(blogId) {
    document.getElementById('deleteId').value = blogId;
    var deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));
    deleteModal.show();
}
</script>

<%@ include file="/WEB-INF/jsp/common/footer.jsp" %> 